<?php

function get_key($addr) {
  return implode('.', array_slice(explode('.', $addr), 0, 3));
}

require_once "../config.php";
require_once "../util.php";
connect_db();

$addr = trim($_POST['addr']);
$port = trim($_POST['port']);
$user = trim($_POST['user']);
$passwd = trim($_POST['passwd']);

if (!$addr || !$port || !$user || !$passwd) {
  die("incomplete proxy info");
}

$cur_key = get_key($addr);

$sql = "select addr from proxy";
$ret = mysql_query($sql);
if (!$ret) {
  die("$sql failed: " . mysql_error());
}
while ($ori_addr = mysql_fetch_row($ret)) {
  if ($cur_key == get_key($ori_addr[0])) {
    die("conflicting with existing IP:({$ori_addr[0]}) <br /> your IP is $addr");
  }
}

$sql = "insert into proxy(addr, port, user_name, password) values('$addr', $port, '$user', '$passwd')";
$ret = mysql_query($sql);
if (!$ret) {
  die("$sql failed: " . mysql_error());
} else {
  echo mysql_affected_rows() . " rows affected";
  return;
}

?>

